﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin.Accounts.Node
{
    public partial class Modify : PageBase
    {
        private string NodeID
        {
            get
            {
                if (Request.QueryString["nodeID"] == null)
                {
                    return "";
                }
                else
                {
                    return Request.QueryString["nodeID"].ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (NodeID == "")
            {
                Response.Write("传入不正确");
                Response.End();
                return;
            }

            if (!Page.IsPostBack)
            {
                if (NodeID != "")
                {
                    BindParentNode();

                    ShowInfo();
                }
            }
        }

        private void BindParentNode()
        {
            List<Vincent.Permission.Model.account_nodeslist> lst = Vincent.Permission.Functions.Account.GetMenus("");// new BLL.account_nodeslist().GetModelList("");
            List<Vincent.Permission.Model.account_nodeslist> lstRoot = lst.Where(p => p.ParentID == "").OrderBy(p => p.OrderID).ToList();

            List<Vincent.Permission.Model.account_nodeslist> lstSource = new List<Vincent.Permission.Model.account_nodeslist>();
            foreach (Vincent.Permission.Model.account_nodeslist node in lstRoot)
            {
                lstSource.Add(node);
                ResetNode(lst, node, lstSource, 1);
            }

            BindDrowDownList(ddlParentNode, lstSource, "Text", "NodeID", true);
        }

        private void ResetNode(List<Vincent.Permission.Model.account_nodeslist> lstAll, Vincent.Permission.Model.account_nodeslist rootNode, List<Vincent.Permission.Model.account_nodeslist> lstSource, int level)
        {
            List<Vincent.Permission.Model.account_nodeslist> lstRoot = lstAll.Where(p => p.ParentID == rootNode.NodeID).OrderBy(p => p.OrderID).ToList();
            foreach (Vincent.Permission.Model.account_nodeslist node in lstRoot)
            {
                string startFlag = "";
                for (int i = 0; i < level; i++)
                {
                    startFlag = "--" + startFlag;
                }
                node.Text = startFlag + node.Text;

                lstSource.Add(node);
                ResetNode(lstAll, node, lstSource, level + 1);
            }
        }

        private void ShowInfo()
        {
            Vincent.Permission.Model.account_nodeslist model = Vincent.Permission.Functions.Account.GetSinglMenu(NodeID);// bll.GetModel(NodeID);
            this.txtText.Text = model.Text;
            this.ddlParentNode.SelectedValue = model.ParentID;
            this.txtUrl.Text = model.Url;
            this.txtDescription.Text = model.Description;
            this.txtOrderID.Text = model.OrderID.ToString();
        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.txtText.Text.Trim().Length == 0)
            {
                strErr += "Text不能为空！\\n";
            }
            //if (this.txtParentID.Text.Trim().Length == 0)
            //{
            //    strErr += "ParentID不能为空！\\n";
            //}
            //if (this.txtUrl.Text.Trim().Length == 0)
            //{
            //    strErr += "Url不能为空！\\n";
            //}
            //if (this.txtDescription.Text.Trim().Length == 0)
            //{
            //    strErr += "Description不能为空！\\n";
            //}
            //if (this.txtImgageUrl.Text.Trim().Length == 0)
            //{
            //    strErr += "ImgageUrl不能为空！\\n";
            //}
            //if (this.txtTarget.Text.Trim().Length == 0)
            //{
            //    strErr += "Target不能为空！\\n";
            //}
            if (!PageValidate.IsNumber(txtOrderID.Text))
            {
                strErr += "OrderID格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string Text = this.txtText.Text;
            string ParentID = this.ddlParentNode.SelectedValue;
            string Url = this.txtUrl.Text;
            string Description = this.txtDescription.Text;
            int OrderID = int.Parse(this.txtOrderID.Text);


            Vincent.Permission.Model.account_nodeslist model = new Vincent.Permission.Model.account_nodeslist();
            model.NodeID = NodeID;
            model.Text = Text;
            model.ParentID = ParentID;
            model.Url = Url;
            model.Description = Description;
            model.OrderID = OrderID;

            Vincent.Permission.Functions.Account.UpdateMenu(model);
            //BLL.account_nodeslist bll = new BLL.account_nodeslist();
            //bll.Update(model);
            MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}